//
// Created by konglinglong on 2020/9/24.
//

#include <stdio.h>
#include <iostream>
#include "../lib/doctest.h"
#include "../../include/osal/osUdp.h"

TEST_SUITE ("OS_UDP_TS")
{
    TEST_CASE ("OS_UDP_TC")
    {
        int32_t rc;
        int sockFd1;
        int sockFd2;
        uint32_t loop;
        uint32_t dataSend[2048];
        uint32_t dataRecv[2048];

        for (loop = 0; loop < ARRAY_SIZE(dataSend); ++loop)
        {
            dataSend[loop] = loop;
        }

        SUBCASE("OS_UDP_SC_BASE")
        {
            rc = OS_UdpInit(&sockFd1);
            REQUIRE(CSP_RC_OK == rc);
            rc = OS_UdpBind(sockFd1, "127.0.0.1", (uint16_t)45678);
            REQUIRE(CSP_RC_OK == rc);
            rc = OS_UdpConnect(sockFd1, "127.0.0.1", (uint16_t)87654);
            REQUIRE(CSP_RC_OK == rc);

            rc = OS_UdpInit(&sockFd2);
            REQUIRE(CSP_RC_OK == rc);
            rc = OS_UdpBind(sockFd2, "127.0.0.1", (uint16_t)87654);
            REQUIRE(CSP_RC_OK == rc);
            rc = OS_UdpConnect(sockFd2, "127.0.0.1", (uint16_t)45678);
            REQUIRE(CSP_RC_OK == rc);

            rc = send(sockFd1, dataSend, sizeof(dataSend), MSG_DONTWAIT);
            REQUIRE(sizeof(dataSend) == rc);
            rc = recv(sockFd2, dataRecv, sizeof(dataRecv), MSG_DONTWAIT);
            REQUIRE(sizeof(dataSend) == rc);
            for (loop = 0; loop < ARRAY_SIZE(dataSend); ++loop)
            {
                REQUIRE(dataRecv[loop] == loop);
            }

            rc = OS_UdpFinal(sockFd1);
            REQUIRE(CSP_RC_OK == rc);
            rc = OS_UdpFinal(sockFd2);
            REQUIRE(CSP_RC_OK == rc);
        }
    }
}
